/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.dialogs;

import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import dgbgui.modeldocument.AttributePrototype;
import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.primitives.ConceptronType;
import dgbgui.modeldocument.uimodels.ContainsEqualComboModel;
import dgbgui.tools.DialogedTool;

@SuppressWarnings("serial")
public class AssociatedDialog extends DGBGUIDialog {
    
    public AssociatedDialog(ConceptronType sourceConceptronType, boolean showInstructions, Set<AttributePrototype> protos, DialogedTool tool) {
        super(tool,"Find associated items");
        setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
        
        Set<String> attribNames = ModelDocument.getInstance().getAttributeNames(sourceConceptronType);

        int numRows = attribNames.size();
        JPanel attributePanel = new JPanel(new GridLayout(numRows,3));
        Border panelBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        attributePanel.setBorder(BorderFactory.createTitledBorder(panelBorder,"Attribute filters"));
        
        for ( String name : attribNames ) {
            JLabel nameLabel = new JLabel(name);

            ContainsEqualComboModel comboModel = new ContainsEqualComboModel();
            JTextField filter = new JTextField();
            if ( protos != null ) {
                for (AttributePrototype proto : protos) {
                    if ( proto.getName().equals(name) ) {
                        comboModel = new ContainsEqualComboModel(proto.getFilterType());
                        filter.setText(proto.getFilterValue());
                        break;
                    }
                }
            }
                
            JComboBox typeCombo = new JComboBox(comboModel);

            
            m_textFieldMap.put(name,filter);
            m_comboModelMap.put(name,comboModel);

            attributePanel.add(nameLabel);
            attributePanel.add(typeCombo);
            attributePanel.add(filter);

        }
        add(attributePanel);
        
        if ( showInstructions ) {
            // Add the instructions
            JPanel instructionsPanel = new JPanel();
            instructionsPanel.setBorder(BorderFactory.createTitledBorder("Instructions"));
            // No supprt for multiline labels, must use html. :(
            JLabel instructions = new JLabel(
                    "<html>Select the relation that defines the<br>" +
                    "association you want to find. Only a<br>" +
            "single relation can be selected.</html>");
            instructionsPanel.add(instructions);
            add(instructionsPanel);
        }
        
        // Add the OK and Cancel buttons
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BoxLayout(buttonsPanel,BoxLayout.X_AXIS));
        m_okButton = new JButton("OK");

        m_cancelButton = new JButton("Cancel");
        buttonsPanel.add(m_okButton);
        buttonsPanel.add(m_cancelButton);

        m_okButton.addActionListener(tool);
        m_cancelButton.addActionListener(tool);

        add(buttonsPanel);
    }
    

    public void setAttributePrototypes(Set<AttributePrototype> protos) {
        for ( AttributePrototype proto : protos ) {
            m_textFieldMap.get(proto.getName()).setText(proto.getFilterValue());
            m_comboModelMap.get(proto.getName()).setSelectedItem(proto.getFilterType());
        }
    }

    
    public String getAttributeFilterString(String name) {
        return m_textFieldMap.get(name).getText();
    }
    
    public AttributePrototype.FilterType getAttributeFilterType(String name) {
        return m_comboModelMap.get(name).getSelected();
    }

    private Map<String,JTextField> m_textFieldMap = new HashMap<String,JTextField>();
    private Map<String,ContainsEqualComboModel> m_comboModelMap = new HashMap<String,ContainsEqualComboModel>();
}
